package link.ten_fei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.service.ChangePwdService;
import link.ten_fei.service.ChangePwdServiceImpl;
import link.ten_fei.util.CookieUtil;
import link.ten_fei.util.MD5Util;

/**
 * Servlet implementation class ChangePwdServlet
 */
@WebServlet("/changePwd.action")
public class ChangePwdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangePwdServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		ChangePwdService changePwdService = new ChangePwdServiceImpl();
		String action = request.getParameter("action");
		// 旧用户名与新用户名
		String olduser = request.getParameter("olduser");
		String user = request.getParameter("user");
		// 用户id与新旧密码
		String id = request.getParameter("doc_id");
		String oldpwd = MD5Util.encodeByMD5(request.getParameter("oldpwd"));
		String pwd = MD5Util.encodeByMD5(request.getParameter("pwd"));
		Doc_User doc_User = (Doc_User) request.getSession().getAttribute("session_user");
		if (action == null) {
			request.setAttribute("mainRight", "/WEB-INF/jsp/changePwd.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		} else if (action != null && action.equals("ajaxUser")) {
			if (doc_User.getDoc_username().equals(olduser) == false) {
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				writer.print("用户名输入错误！！！");
				return;
			}
			List<String> users = changePwdService.findUsername(user);
			if (users.size() >= 1) {
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				writer.print("新用户存在！！！");
				return;
			}
		} else if (action != null && action.equals("changeUsr")) {
			int flag = changePwdService.changeUsrAndPwd(user, Integer.parseInt(id), null);
			// 清除保存在session中的用户信息
			request.getSession().removeAttribute("session_user");
			// 同时也清除保存在cookie中的用户信息
			Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass");
			if (cookie != null) {
				cookie.setMaxAge(0);
				cookie.setPath(request.getContextPath());
				// 将cookie响应出去
				response.addCookie(cookie);
			}
			response.sendRedirect("index.jsp?success_user=" + flag);
			return;
		} else if (action != null && action.equals("ajaxPwd")) {
			if (doc_User.getDoc_password().equals(oldpwd) == false) {
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				writer.print("旧密码输入错误！！！");
				return;
			}
		} else if (action != null && action.equals("changePwd")) {
			int flag = changePwdService.changeUsrAndPwd(null, Integer.parseInt(id), pwd);
			// 清除保存在session中的用户信息
			request.getSession().removeAttribute("session_user");
			// 同时也清除保存在cookie中的用户信息
			Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass");
			if (cookie != null) {
				cookie.setMaxAge(0);
				cookie.setPath(request.getContextPath());
				// 将cookie响应出去
				response.addCookie(cookie);
			}
			response.sendRedirect("index.jsp?success_pwd=" + flag);
			return;
		}
	}
}
